<template>
  <div>
    <!-- 位置栏 -->
    <div class="location-bar">
      <div>位置 :&ensp;</div>
      <div>一级页面</div>
      <div style="color: #000">/二级页面</div>
    </div>
    <!-- 查询框 -->
    <div class="search-bar">
      <a-form-model layout="inline" :model="searchForm" @submit="searchSubmit" @submit.native.prevent>
        <a-form-model-item label="用户名:">
          <a-input v-model="searchForm.name" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" html-type="submit"><a-icon type="search" /></a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
    <!-- 内容框 -->
    <div class="content-box">
      <!-- 新增 -->
      <a-button type="primary" @click="add_modal"> 新增 </a-button>
      <!-- 表格 -->
      <a-table :columns="columns" :data-source="data" @change="handleTableChange" :pagination="pagination" :row-key="(record) => record.id">
        <span slot="status" slot-scope="text, record">
          <a-switch default-checked @change="onChange(record)" :checked="text === '正常' ? true : false" />
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="modify_modal(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="reset_modal(record)"> 重置密码 </a>
          <a-divider type="vertical" />
          <a @click="del(record)"> 删除 </a>
        </span>
      </a-table>
      <!-- 弹窗编辑表单 -->
      <!-- 修改用户 -->
      <a-modal :visible="modal_visible" title="修改用户" okText="确认" cancel-text="取消" @ok="modal_ok" @cancel="modal_cancel">
        <!--表单 并将表单的值绑定到this.from-->
        <a-form :form="modalform" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-form-item label="用户名">
            <a-input v-decorator="['account', { initialValue: modal_message.account , rules: [{ required: true, message: '请输入用户名!' }] }]" />
          </a-form-item>
          <a-form-item label="密码">
            <a-input v-decorator="['pwd', { rules: [{ required: true, message: '请输入密码!' }] }]" />
          </a-form-item>
          <a-form-item label="重复密码">
            <a-input v-decorator="['enterPwd', { rules: [{ required: true, message: '请输入重复密码!' }] }]" />
          </a-form-item>
          <a-form-item label="状态">
            <a-radio-group v-decorator="[ 'status', { initialValue: modal_message.status, rules: [{ required: true, message: '请选择!' }] }]">
              <a-radio value="正常">启用</a-radio>
              <a-radio value="禁用">禁用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </a-modal>
      <!-- 新增用户 -->
      <a-modal :visible="add_visible" title="新增用户" okText="确认" cancel-text="取消" @ok="add_ok" @cancel="add_cancel">
        <!--表单 并将表单的值绑定到this.from-->
        <a-form :form="addform" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-form-item label="用户名">
            <a-input v-decorator="['account', { rules: [{ required: true, message: '请输入用户名!' }] }]" />
          </a-form-item>
          <a-form-item label="密码">
            <a-input v-decorator="['pwd', { rules: [{ required: true, message: '请输入密码!' }] }]" />
          </a-form-item>
          <a-form-item label="重复密码">
            <a-input v-decorator="['enterPwd', { rules: [{ required: true, message: '请输入重复密码!' }] }]" />
          </a-form-item>
          <a-form-item label="状态">
            <a-radio-group v-decorator="[ 'status', { rules: [{ required: true, message: '请选择!' }] }]">
              <a-radio value="正常">启用</a-radio>
              <a-radio value="禁用">禁用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-form>
      </a-modal>
      <!-- 修改密码 -->
      <a-modal :visible="reset_visible" title="重置密码" okText="确认" cancel-text="取消" @ok="reset_ok" @cancel="reset_cancel">
        <!--表单 并将表单的值绑定到this.from-->
        <a-form :form="resetform" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }">
          <a-form-item label="旧密码">
            <a-input v-decorator="['oldpassword', { rules: [{ required: true, message: '请输入旧密码!' }] }]" />
          </a-form-item>
          <a-form-item label="新密码">
            <a-input v-decorator="['password', { rules: [{ required: true, message: '请输入新密码!' }] }]" />
          </a-form-item>
          <a-form-item label="重复密码">
            <a-input v-decorator="['passwords', { rules: [{ required: true, message: '请输入重复密码!' }] }]" />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
  </div>
</template>

<script>
/**表头信息 */
const columns = [
  {
    title: '序号',
    dataIndex: 'id',
    key: 'id',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '用户名',
    dataIndex: 'account',
    key: 'account',
    scopedSlots: { customRender: 'account' },
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '创建时间',
    key: 'loginDate',
    dataIndex: 'loginDate',
    scopedSlots: { customRender: 'loginDate' },
  },
  {
    title: '操作',
    key: 'action',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
  },
];
export default {
  beforeCreate() {
    //创建表单
    this.addform = this.$form.createForm(this, { name: 'addform' });
    this.modalform = this.$form.createForm(this, { name: 'modalform' });
    this.resetform = this.$form.createForm(this, { name: 'resetform' });
  },
  data() {
    return {
      columns,
      data: [],
      searchForm: { name: '' }, //查询参数
      pagination: { pageSize: 10, total: 100, pageNum: 1, showSizeChanger: true }, //分页参数
      modal_visible: false,
      modal_message: '', //被编辑信息
      modal_form: '', //编辑内容
      add_visible: false,
      add_form: '',
      reset_visible: false,
      reset_message: '',
      reset_form: '',
      del_id: '', //获取删除id
    };
  },
  created() {
    this.loadArticleList();
  },
  methods: {
    //查询
    async loadArticleList() {
      let reqData = { searchText: this.searchForm.name, startTime: '', endTime: '' };
      let reqParam = { pageNumber: this.pagination.pageNum, pageSize: this.pagination.pageSize };
      let searchUrl = `/user/search/${reqParam.pageNumber}/${reqParam.pageSize}`;
      let result = await this.axios.post(searchUrl, reqData);
      console.log(result);
      this.data = result.data.data;
      this.pagination.total = result.data.total; //总数
      // console.log(this.data)
      this.search_empty();
    },
    //新增
    async newlyAdded() {
      let reqData = { pwd: this.add_form.pwd, account: this.add_form.account, enterPwd: this.add_form.enterPwd, status: this.add_form.status, };
      let saveUrl = `/user/save`;
      let result = await this.axios.post(saveUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.addform.resetFields();
    },
    //改
    async modifyList() {
      let reqData = { loginTimes: 0, id: this.modal_form.id, pwd: this.modal_form.pwd, account: this.modal_form.account, token: this.store.admin.token, status: this.modal_form.status };
      let updateUrl = `/user/update`;
      let result = await this.axios.post(updateUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.modalform.resetFields();
    },
    //删除
    async deleteList() {
      let reqData = {};
      let reqParam = { id: this.del_id };
      let deleteUrl = `/user/delete/${reqParam.id}`;
      let result = await this.axios.delete(deleteUrl, reqData);
      console.log(result);
      this.loadArticleList();
    },
    //搜索
    searchSubmit() {
      this.loadArticleList();
    },
    // 分页
    handleTableChange(pagination) {
      this.pagination.pageNum = pagination.current
      this.loadArticleList();
    },
    //状态开关
    onChange(record) {
      if (record.status == '正常') {
        record.status = '禁用';
      } else {
        record.status = '正常';
      }
      this.modal_form = record;
      this.modifyList();
      // console.log(this.modal_form);
    },
    //修改
    //显示模态框
    modify_modal(record) {
      this.modal_message = record;
      this.modal_visible = true;
    },
    //关闭模态框
    modal_cancel() {
      this.modal_visible = false;
      this.modalform.resetFields();
    },
    //点击确认
    modal_ok() {
      const form = this.modalform;
      form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (values.pwd == values.enterPwd) {
          this.modal_form = values;
          this.modal_form.id = this.modal_message.id;
          // console.log(this.modal_form);
          this.modal_visible = false;
          this.modifyList();
        } else {
          this.$notification.warning({
            message: '密码不一致',
            duration: 1.5,
          });
        }
      });
    },
    //新增
    add_modal() {
      this.add_visible = true;
    },
    add_cancel() {
      this.add_visible = false;
      this.addform.resetFields();
    },
    add_ok() {
      const form = this.addform;
      form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (values.pwd == values.enterPwd) {
          this.add_form = values;
          // console.log(this.add_form);
          this.add_visible = false;
          this.newlyAdded();
        } else {
          this.$notification.warning({
            message: '密码不一致',
            duration: 1.5,
          });
        }
      });
    },
    //重置密码
    reset_modal(record) {
      this.reset_message = record;
      this.reset_visible = true;
    },
    reset_cancel() {
      this.reset_visible = false;
    },
    reset_ok() {
      const form = this.resetform;
      form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (values.password == values.passwords) {
          this.reset_form = values;
          this.reset_form.id = this.reset_message.id;
          console.log(this.reset_form);
          form.resetFields();
          this.reset_visible = false;
        } else {
          this.$notification.warning({
            message: '密码不一致',
            duration: 1.5,
          });
        }
      });
    },
    //删除
    del(record) {
      this.del_id = record.id;
      this.deleteList();
    },
    //清空搜索
    search_empty() {
      this.searchForm.name='';
    }
  },
};
</script>

<style>
.location-bar {
  display: flex;
  font-size: 14px;
  color: #88898b;
}
.search-bar {
  width: 100%;
  height: 80px;
  background-color: #ffffff;
  margin-top: 17px;
  box-sizing: border-box;
  padding: 20px 30px;
}
.content-box {
  width: 100%;
  background-color: #ffffff;
  margin-top: 20px;
  box-sizing: border-box;
  padding: 20px 30px;
}
.ant-modal-footer {
  text-align: center;
}
</style>